
angular.module('Demo')

.controller('Demo.SerialCtrl', ['$scope', '$state', '$ionicScrollDelegate', '$timeout', 'SerialPort', 'DevicePort', 'imanDialog', 
    function ($scope, $state, $ionicScrollDelegate, $timeout, SerialPort, DevicePort, imanDialog) {
        $scope.data = {
            deviceName: '',
            baudrate: 115200,
            format: 'Hex',
            autoClear: false,
            portState: '打开',
            dataArray: []
        };

        var _deviceId;

        $scope.selectDevice = function () {
            var externalDevices = SerialPort.getExternalDevices(true);
            if (externalDevices.length === 0) {
                imanDialog.confirm('请去设置界面绑定串口', '串口设置').then(function (btn) {
                    if (btn) {
                        $state.go('portsetting');
                    }
                });
                return;
            }
            imanDialog.showList({
                title: '请选择设备',
                list: externalDevices,
                valueField: 'Device',
                displayField: 'Name',
                vaule: _deviceId
            }).then(function (select) {
                _deviceId = select.Device;
                $scope.data.deviceName = select.Name;
            });
        };

        $scope.selectBaudrate = function () {
            var baudrateList = [
                { rate: 50 }, { rate: 75 }, { rate: 110 }, { rate: 134 }, { rate: 150 },
                { rate: 200 }, { rate: 300 }, { rate: 600 }, { rate: 1200 }, { rate: 1800 }, { rate: 2400 },
                { rate: 4800 }, { rate: 9600 }, { rate: 19200 }, { rate: 38400 }, { rate: 57600 }, 
                { rate: 115200 }, { rate: 230400 }, { rate: 460800 }, { rate: 500000 }, { rate: 576000 },
                { rate: 921600 }, { rate: 1000000 }, { rate: 1152000 }, { rate: 1500000 }, { rate: 2000000 }, 
                { rate: 2500000 }, { rate: 3000000 }, { rate: 3500000 }, { rate: 4000000 }
            ];
            imanDialog.showList({
                title: '请选择波特率',
                list: baudrateList,
                valueField: 'rate',
                displayField: 'rate',
                vaule: $scope.data.baudrate
            }).then(function (select) {
                $scope.data.baudrate = select.rate;
            });
        };

        $scope.selectFormat = function () {
            var formatList = [{ format: 'Txt' }, { format: 'Hex' }];
            imanDialog.showList({
                title: '请选择输出格式',
                list: formatList,
                valueField: 'format',
                displayField: 'format',
                vaule: $scope.data.format
            }).then(function (select) {
                $scope.data.format = select.format;
            });
        };

        $scope.portOperate = function () {
            if ($scope.data.portState === '打开') {
                if (!_deviceId) {
                    imanDialog.toastBottom('请选择设备');
                    return;
                }
                DevicePort.open({
                    device: _deviceId,
                    baudrate: $scope.data.baudrate
                });
                $scope.data.portState = '关闭';
            } else {
                DevicePort.close();
                $scope.data.portState = '打开';
            }
        };

        $scope.clearData = function () {
            $scope.data.dataArray = [];
            $ionicScrollDelegate.$getByHandle('demoSerialScroll').resize();
        };

        $scope.$on('devicePort.DataReceived', function (e, data) {
            if ($scope.data.format === 'Txt') {
                data = convertToTxt(data);
            }
            if ($scope.data.autoClear) {
                $scope.data.dataArray = [data];
            } else {
                $scope.data.dataArray.unshift(data);
            }
            $timeout(function () {
                $scope.apply();
            });
        });

        $scope.$on('$ionicView.beforeLeave', function () {
            DevicePort.close();
        });

        function convertToTxt (data) {
            var result = '';
            for (var i = 0; i < data.length; i += 2) {
                result += String.fromCharCode(parseInt(data.substr(i, 2), 16));
            }
            return result;
        }
    }
]);